package com.zzyq.pc.web.configure.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.common.plugin.shiro.TokenManager;
import com.zzyq.pc.web.configure.mapper.EnshrineMapper;
import com.zzyq.pc.web.configure.mapper.InformationMapper;
import com.zzyq.pc.web.configure.model.Enshrine;
import com.zzyq.pc.web.configure.model.Information;
import com.zzyq.pc.web.configure.service.IEnshrineService;

@Service
public class EnshrineServiceImpl implements IEnshrineService{
	
	@Autowired
	private EnshrineMapper enshrineMapper;
	@Autowired
	InformationMapper informationMapper;
	
	private Integer figureFive = 5;
	private Integer figureTen = 10;
	private Integer figureFifteen = 15;
	private Integer figureTwenty = 20;
	
	/**
	 * 新增
	 * @param record
	 * @return
	 */
	@Override
	public boolean insert(Enshrine record){
		//收藏时间默认为当前时间
		record.setEnshrineTime(new Date(System.currentTimeMillis()));
		//获取当前用户ID
		record.setUserId(TokenManager.getUser().getUserId());
		//record.setReleaseTime(new Date(System.currentTimeMillis()));
		
		return enshrineMapper.insert(record)>0;
	}
	
	/**
	 * 删除
	 */
	@Override
	public boolean deleteById(String ifmId,String articleId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ifmId", ifmId);
		map.put("articleId", articleId);
		return enshrineMapper.delete(map)>0;
	}
	
	/**
	 * 查询列表
	 */
	@Override
	public List<Enshrine> selectAll(PageView pageView,String enshrineTitle){
		List<Enshrine> enshrine = new ArrayList<Enshrine>();
		//获取用户ID
		int userId = TokenManager.getUser().getUserId();
		//获取当前团体ID
		int companyId = TokenManager.getUser().getCompany().getCompanyId();
		Map<String,Object> map2 = new HashMap<String, Object>();
		map2.put("companyId", companyId);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("pageView",pageView);
		map.put("enshrineTitle", enshrineTitle);
		map.put("userId", userId);
		List<Enshrine> esh = enshrineMapper.selectAll(map);
		//敏感方案ID集合
		List<String> ifmIds2 = new ArrayList<String>();
		//根据团体ID查询所有方案集合
		List<Information> info = informationMapper.selectInformationByCompanyId(map2);
		for (Information tion : info) {
			//所有负面方案
			if (tion.getIfmIssensitivity()==1) {
				ifmIds2.add(String.valueOf(tion.getIfmId()));
			} /*else {
				ifmIds1.add(String.valueOf(tion.getIfmId()));
			}*/
		}
		//所有已收藏信息集合
		for (Enshrine e : esh) {
			for (String mgids : ifmIds2) {
				if (e.getIfmId().equals(mgids)) {
					//相等，设置改数据为负面
					e.setEnshrineNature("负面");
					//判断星级 只有负面方案的信息需要判断星级
					if (!e.getArticleType().equals("微信") && !e.getArticleType().equals("微博") 
							&& !e.getArticleType().equals("论坛")) {
						//四星
						e.setStarLevel("★★★★");
					} else {
						if (e.getReprInt()<=figureFive) {
							//一星
							e.setStarLevel("★");
						} else if (e.getReprInt()>figureFive && e.getReprInt()<=figureTen) {
							//两星
							e.setStarLevel("★★");
						} else if (e.getReprInt()>figureTen && e.getReprInt()<=figureFifteen) {
							//三星
							e.setStarLevel("★★★");
						} else if (e.getReprInt()>figureFifteen && e.getReprInt()<=figureTwenty) {
							//四星
							e.setStarLevel("★★★★");
						} else if (e.getReprInt()>figureTwenty) {
							//五星
							e.setStarLevel("★★★★★");
						}
					}
				} /*else {
					e.setEnshrineNature("正面");
					e.setStarLevel("");
				}*/
			}
			if (StringUtils.isBlank(e.getEnshrineNature())) {
				e.setEnshrineNature("正面");
				e.setStarLevel("");
			}
			enshrine.add(e);
		}
		return enshrine;
	}
	/**
	 * 用于取消批量收藏
	 */
	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		return enshrineMapper.deleteByPrimaryKey(id)>0;
	}
}
